package com.xl.singletonPattern.practice;

import com.xl.singletonPattern.ConfigManager;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DataBaseConnectManager {

    //私有化构造方法
    private DataBaseConnectManager(){
        loadConfig();
    }

    //创建一个静态内部类
    private static class DataBaseConnectManagerHolder{
        private static final DataBaseConnectManager instance = new DataBaseConnectManager();
    }

    //提供静态方法获取单例对象
    public static DataBaseConnectManager getInstance(){
        return DataBaseConnectManagerHolder.instance;
    }

    private Properties config;

    private void loadConfig() {
        config = new Properties();
        try (InputStream input = ConfigManager.class.getClassLoader().getResourceAsStream("config.properties")) {
            if (input != null) {
                config.load(input);
            } else {
                System.out.println("配置文件未找到");
            }
        } catch (IOException e) {
            System.out.println("加载配置文件出错...");
        }
    }

    public String getProperty(String key){
        return config.getProperty(key);
    }

    public void connect(){
        System.out.println("连接数据库...");
    }

    public void close(){
        System.out.println("关闭数据库连接...");
    }
}
